package cn.itcast.web.controller.system;

import cn.itcast.domain.system.Dept;
import cn.itcast.domain.system.Role;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.DeptService;
import cn.itcast.service.system.RoleService;
import cn.itcast.service.system.UserService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.excel.util.StringUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping(value = "/system/user",name = "分页查询用户列表")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private DeptService deptService;

    @Autowired
    private RoleService roleService;

    /**
     * 分页查询用户列表
     *
     */
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") int page,
                        @RequestParam(defaultValue = "10") int size){
        PageInfo info = userService.findAll(getLoginCompanyId(),page,size);
        request.setAttribute("page",info);
        return "/system/user/user-list";
    }

    @RequestMapping(value = "/toAdd",name = "进入保存用户")
    public String toadd(){
        //查询所有部门
        List<Dept> deptList = deptService.findAll(getLoginCompanyId());
        request.setAttribute("deptList",deptList);
        return "/system/user/user-add";
    }

    /**
     * 保存或者更新用户
     *
     */
    @RequestMapping(value = "/edit",name = "保存或者更新用户")
    public String edit(User user){
        //设置企业参数
        user.setCompanyId(getLoginCompanyId());
        user.setCompanyName(getLoginCompanyName());
        //判断是否具有id
        if (StringUtils.isEmpty(user.getId())){
            //没有id保存
            userService.save(user);
        }else {
            //有id更新
            userService.update(user);
        }
        return "redirect:/system/user/list.do";
    }

    /**
     * 更新用户
     *
     */
    @RequestMapping(value = "/toUpdate",name = "更新用户")
    public String toUpdate(String id){
        //调用service根据id查询用户
        User user = userService.findById(id);
        request.setAttribute("user",user);
        //查询所有部门
        List<Dept> deptList = deptService.findAll(getLoginCompanyId());;
        request.setAttribute("deptList",deptList);
        return "/system/user/user-update";
    }

    /**
     * 更新用户
     *
     */
    @RequestMapping(value = "/delete",name = "删除用户")
    public String delete(String id){
        //调用service删除
        userService.delete(id);
        return "redirect:/system/user/list.do";
    }

    /**
     * 进入角色分配页面
     */
    @RequestMapping("/roleList")
    public String roleList(String id){

        //根据id查询用户
        User user = userService.findById(id);
        request.setAttribute("user",user);
        //查询所有角色
        List<Role> roleList = roleService.findAll(getLoginCompanyId());
        request.setAttribute("roleList",roleList);
        //根据用户查询id，查询此用户的所有角色id
        List<String> roles = userService.findRolesByUserId(id);
        String userRoleStr = "";
        for (String roleId : roles) {
            userRoleStr += roleId + " ";
        }
        request.setAttribute("userRoleStr",userRoleStr);

        return "system/user/user-role";
    }

    //实现对用户分配角色的功能
    @RequestMapping("/changeRole")
    public String changeRole(String userid,String [] roleIds){
        userService.changeRole(userid,roleIds);

        return "redirect:/system/user/list.do";
    }

}
